class SummaryRanges:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.all = []

    def addNum(self, val: int) -> None:
        i = 0
        while i < len(self.all):
            if self.all[i][0] <= val <= self.all[i][1]:
                return
            if self.all[i][0] > val:
                break
            i = i + 1

        # 往前合并
        hadMerge = False
        if i != 0:
            if val == self.all[i-1][1] + 1:
                self.all[i-1][1]+=1
                hadMerge = True
        # 往后合并
        if not hadMerge and i < len(self.all):
            if val == self.all[i][0] - 1:
                self.all[i][0]-=1
                hadMerge = True
        if hadMerge:
            if 0<i<len(self.all):
                if self.all[i-1][1] + 1 == self.all[i][0]:
                    self.all[i-1][1] = self.all[i][1]
                    self.all.pop(i)
        else:
            self.all.insert(i, [val,val])

    def getIntervals(self):
        return self.all


if __name__ == '__main__':
    obj = SummaryRanges()
    vals = [1, 3, 7, 2, 6]
    for val in vals:
        obj.addNum(val)
        param_2 = obj.getIntervals()
        print(param_2)
